<template>
	<div class='toc_IM' id="app">
		<transition name="slide-fade">
			<router-view class='toc_IM'></router-view>
		</transition>
	</div>
</template>

<script>
	import units from '@/assets/scripts/units.js'
	import dataInterface from '@/assets/scripts/dataInterface.js'
	import Toc_IM_Service from '@/assets/scripts/Toc_IM_Service.js'
	export default {
		name: 'app',
		data(){
	    	return {
	    		imToken:null,
	    	}
	   	},
		mounted() {
			this.imToken = units.getQueryString('imToken') == null?null:units.getQueryString('imToken');
	  		if(this.imToken !=null){
	  			this.loginByToken();
	  		};
			this.createSocketListener();
		},
		computed: {
			currentPageName() {return this.$store.state.routerInfo.currentPageName},
			messageList(){return this.$store.state.messageList.data},
			userId(){return this.$store.state.userId},
		},
		methods: {
			createSocketListener() {
				this.$messageController.watch('ChatMsgSocket_ServerSend', data => {
					this.upDateMsgList(data);
					this.$bus.emit('tocIM_PAGE' + this.currentPageName, data);
				});
				this.$messageController.watch('SystemSocket_ServerCloseForOtherLogin', data => {
					this.$store.commit({
					  	type: 'changeUserLoginState',
					  	isLogin:false
					});
					this.$toast({
					  	message: data.data,
					  	position: 'bottom',
					  	duration: 2000
					});
					this.$router.push({name: 'login'});
				});
			},
			upDateMsgList(data) {
				var _self =this;
				var iscreate = true;
				var getChatID = data.data.chatID;
				_self.messageList.forEach(item => {
					if(getChatID == item.chat._id) {
						iscreate = false
					};
				});
				if(!iscreate) {
					_self.$store.commit({
						type: 'upDateMessageList',
						chatID: getChatID,
						newLastMsg: data.data
					});
				} else {
					units.AJAX.JsonData('get', 'httpChat/chatAndChatMemInfo', {
						memId: _self.userId,
						chatId: getChatID
					}, (mLdata) => {
						_self.$store.commit({
							type: 'topMessageList',
							newMsgItem: mLdata
						});
					});
				};
			}, 
			loginByToken(){
				this.$indicator.open({
				  	text: '正在通过Token登录',
				  	spinnerType: 'fading-circle'
				});
				this.$messageController.emit('SystemSocket_ClientLoginByImToken',{
					imToken:this.imToken,
				},'SystemSocket_ServerLoginedByImToken',(data)=>{
					this.$indicator.close();
					if(data.data!==undefined){
						this.$store.commit({
						  	type: 'changeUserLoginState',
						  	isLogin:true,
						 	userId:data.data.memInfo._id,
						 	accountID:data.data.memInfo.accountID,
						 	avatar:data.data.memInfo.avatar,
						 	name:data.data.memInfo.name,
							userName:data.data.username,
							terminalSize:function(){
								if(units.browers.isMobile()){if(units.browers.isAndroid()){return 'android'}else if(units.browers.isIos()){return 'ios'}}else{return 'pc'}
							}()
						});
						this.$nextTick(()=>{this.getList(data.data.memInfo._id)});
						this.thirdController(data.data);
					}else{
						this.$toast({
						  	message: '登录失败',
						  	position: 'bottom',
						  	duration: 2000
						});
					};	
				});	
			},
			getList(userId){
				dataInterface.chatList({
					memId:userId,
					lastTime:''
				},(res)=>{
					this.$store.commit({
			  			type: 'loadMessageList',
			  			data:res
		  			});
		  			this.$router.push({name: 'messageList'});
				});
			},
			thirdController(thirdInfo){
				if(thirdInfo.modeType == 'share'){
					Toc_IM_Service.share(thirdInfo,this);
				}
			}
			
		}
	}
</script>
<style>
	div#app {
		position: absolute;
		top: 0;
		bottom: 0;
		left: 0;
		right: 0;
	}
</style>